package com.zhongwang.mms.module.buy.listner;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.zhongwang.mms.common.ImportCallBack;
import com.zhongwang.mms.gen.entity.TDictionary;
import com.zhongwang.mms.gen.entity.TEmploy;
import com.zhongwang.mms.module.buy.dao.ReportPlanDao;
import com.zhongwang.mms.module.buy.model.ReportPlanModel;
import com.zhongwang.mms.util.CodeUtil;
import com.zhongwang.mms.util.ShiroUtils;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 提报计划导入 listener
 *
 * @Author: China.wsg
 * @Date: 2020/01/09 16:38
 */
public class ReportPlanListener extends AnalysisEventListener<ReportPlanModel> {

    private List<TEmploy> employs;
    private ReportPlanDao reportPlanDao;
    private ImportCallBack callBack;
    private List<ReportPlanModel> reportPlanModels = new ArrayList<>();
    private List<TDictionary> categories;
    private List<Integer> exceptionList = new ArrayList<>();
    private int i = 2;

    public ReportPlanListener(ReportPlanDao reportPlanDao,
                              ImportCallBack callBack,
                              List<TDictionary> categories,
                              List<TEmploy> employs) {
        this.reportPlanDao = reportPlanDao;
        this.callBack = callBack;
        this.categories = categories;
        this.employs = employs;
    }

    @Override
    public void invoke(ReportPlanModel data, AnalysisContext context) {
        /// 三个数量保留四位小数
        if (data.getRppNum() != null) {
            BigDecimal bd = new BigDecimal(data.getRppNum());
            data.setRppNum(bd.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
        }
//        if (data.getRppRealNum() != null) {
//            BigDecimal bd1 = new BigDecimal(data.getRppRealNum());
//            data.setRppRealNum(bd1.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
//        }
//        if (data.getRppOverNum() != null) {
//            BigDecimal bd2 = new BigDecimal(data.getRppOverNum());
//            data.setRppOverNum(bd2.setScale(4, BigDecimal.ROUND_HALF_UP).doubleValue());
//        }
        // 查分类
        TDictionary type = getType(data.getRppTypes());

        data.setReason("");

        if (data.getRppPlanner() == null) {
            data.setReason(data.getReason() + "计划员没填");
        }
        if (data.getRppMatDesc() == null) {
            data.setReason(data.getReason() + "物料描述没填");
        }
        if (data.getRppNum() == null) {
            data.setReason(data.getReason() + "数量没填");
        }
        if (data.getRppUnit() == null) {
            data.setReason(data.getReason() + "单位没填");
        }
        if (data.getRppProjectCode() == null) {
            data.setReason(data.getReason() + "项目编号没填");
        }
        if (data.getRppProjectName() == null) {
            data.setReason(data.getReason() + "项目名称没填");
        }
        if (data.getRppTypes() == null) {
            data.setReason(data.getReason() + "分类没填");
        }
        if (type == null) {
            data.setReason(data.getReason() + "分类不存在");
        }
        if ( getPlanner(data.getRppPlanner()) == null) {
            data.setReason(data.getReason() + "计划员不存在");
        }
        if ( reportPlanDao.findProjectByCode(data.getRppProjectCode()) == null) {
            data.setReason(data.getReason() + "项目号不存在");
        }
        // 项目号转换八位
        data.setRppProjectCode(CodeUtil.projectCodeHandle(data.getRppProjectCode()));
        if (data.getRppReportStatus() == null) {
            data.setRppUseStock(0);
        } else {
            switch (data.getRppReportStatus()) {
                case "使用库存":
                    data.setRppUseStock(1);
                    break;
                case "正常提报":
                    data.setRppUseStock(2);
                    break;
                case "暂不提报":
                    data.setRppUseStock(3);
                    break;
                default:
                    data.setRppUseStock(0);
            }
        }
        // 追计划标识
        if ("是".equals(data.getRppAppendStr())) {
            data.setRppAppend(1);
        } else {
            data.setRppAppend(0);
        }
        if ( reportPlanDao.findExist(data) > 0) {
            data.setReason(data.getReason() + "重复数据");
        }

        // 验证
        if (data.getReason().length() > 0) {
            reportPlanModels.add(data);
            i++;
            return;
        }

        data.setRppTypeId(type.getDicId());

        // 原始计划
        data.setRppOriginalFlag(1);
        // 正常状态
        data.setRppStatus(2);
        // 可以拆分状态
        data.setRppAllowCancel(2);



        // 创建人和时间
        data.setRppCreateUser(ShiroUtils.getUser().getName());
        data.setRppCreateTime(new Date());
        // 未指定库存状态
        data.setRppIsFlag(0);
        reportPlanDao.insert(data);
        i++;
    }

    /** 获取类型 */
    private TDictionary getType(String rppTypes) {
        for (TDictionary category : categories) {
            if (category.getDicName() != null && category.getDicName().equals(rppTypes)) {
                return category;
            }
        }
        return null;
    }

    /** 获取计划员 */
    private TEmploy getPlanner(String planner) {
        for (TEmploy employ : employs) {
            if (employ.getEmpName() != null && employ.getEmpName().equals(planner)) {
                return employ;
            }
        }
        return null;
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        // 异常数据回写行号
        if (exceptionList.size() > 0) {
            ReportPlanModel model = new ReportPlanModel();
            model.setRppMatDesc( "第" + exceptionList.toString() + "行数据格式不正确");
            reportPlanModels.add(model);
        }
        if (reportPlanModels.size() > 0) {
            callBack.onWarning(reportPlanModels);
        } else {
            callBack.onSuccess();
        }
    }

    @Override
    public void onException(Exception exception, AnalysisContext context) {
        exceptionList.add(i);
        i++;
    }

}